<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外链类型" prop="linkType">
        <el-select
          v-model="queryParams.linkType"
          placeholder="请选择外链类型"
          clearable
        >
          <el-option label="群二维码" :value="1" />
          <el-option label="相关平台链接" :value="2" />
          <el-option label="联盟单位" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tool:config:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tool:config:edit']"
          >修改</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="configList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类型" prop="linkType" align="center">
        <template #default="{ row }">{{
          row.linkType === 1
            ? "二维码"
            : row.linkType === 2
            ? "平台链接"
            : "联盟单位"
        }}</template>
      </el-table-column>
      <el-table-column label="标题" prop="linkTitle"   width="120" show-overflow-tooltip />
      <el-table-column label="图片" prop="linkPicUrl" align="center">
        <template slot-scope="scope">
          <image-preview
            v-if="scope.row.linkPicUrl"
            :src="scope.row.linkPicUrl"
            :width="50"
            :height="50"
          />
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="跳转链接" prop="openUrl" align="center" >
        <template #default="{ row }">{{ row.openUrl || "无" }}</template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status"   width="120">
        <template slot-scope="{ row }">
          <span v-if="row.status === -1">未审核</span>
          <span v-else-if="row.status === -2">已驳回</span>
          <template v-else>
            <el-switch
              :value="row.status"
              :active-value="1"
              :inactive-value="0"
              @change="toggleStatus(row)"
            ></el-switch>
            <span>{{ row.status ? "上架" : "下架" }}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createBy" align="center"/>
      <el-table-column label="创建时间" prop="createTime" align="center" />
      <el-table-column label="备注" prop="remark"   width="120" show-overflow-tooltip />
      <el-table-column
        label="操作"
        align="center"
        width="120"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tool:config:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改需求信息对话框 -->
    <el-dialog    :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="类型" prop="linkType">
          <el-select v-model="form.linkType" placeholder="请选择类型" clearable>
            <el-option :value="1" label="二维码"></el-option>
            <el-option :value="2" label="平台链接"></el-option>
            <el-option :value="3" label="联盟单位"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="linkTitle">
          <el-input
            v-model="form.linkTitle"
            placeholder="请输入标题"
            :maxlength="10"
          />
        </el-form-item>
        <el-form-item label="图片" prop="linkPicUrl">
          <minio-image-upload v-model="form.linkPicUrl" :limit="1" />
        </el-form-item>
        <el-form-item label="跳转链接" prop="openUrl">
          <el-input v-model="form.openUrl" placeholder="请输入跳转链接" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getConfigList,
  addConfig,
  editConfig,
  toggleStatus,
  getDetail,
} from "@/api/config";

export default {
  name: "HomeConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 需求信息表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: "",
        linkType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        linkType: [{ required: true, message: " 请选择类型" }],
        linkTitle: [{ required: true, message: "请输入标题" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async toggleStatus(row) {
      const { id, status } = row;
      const { code } = await toggleStatus({
        id,
        status: Number(!status),
      });
      if (code === 200) {
        this.$message.success(status ? "下架成功" : "上架成功");
        this.$set(row, "status", Number(!status));
      }
    },
    /** 查询需求信息列表 */
    getList() {
      this.loading = true;
      getConfigList(this.queryParams).then((response) => {
        this.configList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        linkPicUrl: null,
        linkTitle: null,
        linkType: null,
        openUrl: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDetail(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            editConfig(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
